<?php
/**
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * @author Eddie Drapkin <eddie@frameweld.com>
 * @license http://www.gnu.org/licenses/lgpl-3.0.txt
 * @package Solphr
 * @version 0.1-pre
 */

require_once dirname(__FILE__) . '/Solphr_Record.php';
require_once dirname(__FILE__) . '/Solphr_Exception.php';
require_once dirname(__FILE__) . '/Request/Solphr_Request_Interface.php';
require_once dirname(__FILE__) . '/Request/Solphr_Request_Curl.php';
require_once dirname(__FILE__) . '/Solphr_SimpleAction.php';

/**
 * DOCUMENT ME!
 * @author Eddie Drapkin <eddie@frameweld.com>
 * @license http://www.gnu.org/licenses/lgpl-3.0.txt
 * @package Solphr
 */
 
class Solphr_AddUpdate extends Solphr_SimpleAction {
	private $records = array();
	
	public function addRecord(Solphr_Record $record) {
		$this->records[] = (string)$record;
		
		if($this->autoCommit === true) {
			$this->submitRecords();
			return $this->commit();
		}
	}
	
	public function submitRecords() {
		if(empty($this->records)) {
			return false;
		}
		
		$req = '<add';
		
		if($this->allowDups === true) {
			$req .= ' allwDups="true"';
		}
		
		$req .= '>';
		
		foreach($this->records as $r) {
			$req .= $r;
		}
		
		$req .= '</add>';
		
		$this->req->setDataFromString($req, true);
		$this->records = array();
		return $this->req->commit();
	}
}